package com.dp.show.www;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.MenuItem;

import com.dp.show.www.databinding.ActivityMainBinding;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.gyf.immersionbar.ImmersionBar;
import com.lib.showfield.app.AppConfig;
import com.lib.showfield.base.adaper.BaseFragmentAdapter;
import com.lib.showfield.common.MyActivity;

public class MainActivity extends MyActivity
        implements BottomNavigationView.OnNavigationItemSelectedListener {

    private ActivityMainBinding mainBinding;
    private ViewPager mViewPager;
    private BottomNavigationView mBottomNavigationView;
    private BaseFragmentAdapter<Fragment> mPagerAdapter;
    public static int mCurrentIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppConfig.initSdk(getActivity(), "https://gateway-test.upingu.cn",
                "https://h5-test.upingu.cn",
                "APP1594644083423");

        ImmersionBar.with(this).statusBarColor(R.color.white).fitsSystemWindows(true)
                .statusBarDarkFont(true)
                .init();
    }

    @Override
    protected void getLayoutId() {
        mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        mViewPager = mainBinding.vpMainPager;
        mBottomNavigationView = mainBinding.bvHomeNavigation;
    }

    @Override
    protected void initView() {
        // 不使用图标默认变色
        mBottomNavigationView.setItemIconTintList(null);
        mBottomNavigationView.setOnNavigationItemSelectedListener(this);

        mCurrentIndex = 0;

    }

    @Override
    protected void initData() {
        mPagerAdapter = new BaseFragmentAdapter<>(this);
        mPagerAdapter.addFragment(new HomeFragment());
        mPagerAdapter.addFragment(new HomeFragment());

        mViewPager.setAdapter(mPagerAdapter);

        // 限制页面数量
        mViewPager.setOffscreenPageLimit(1);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_home:
                ImmersionBar.with(this).statusBarColor(R.color.white).fitsSystemWindows(true)
                        .statusBarDarkFont(true)
                        .init();
                mPagerAdapter.setCurrentItem(HomeFragment.class);

                return true;
            case R.id.home_found:

                ImmersionBar.with(this).statusBarColor(R.color.maincolorDark).fitsSystemWindows(true)
                        .statusBarDarkFont(false)
                        .init();
                mPagerAdapter.setCurrentItem(1);
                return true;
            default:
                break;
        }
        return false;
    }
}